﻿using Microsoft.AspNetCore.Authorization;

namespace TestGrpc.Auth
{
    public class PermissionKeyHandler : AuthorizationHandler<PermissionRequirement>
    {


        protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, PermissionRequirement requirement)
        {

            var value =context.User.FindFirst("Grpc");
            if(value is null&& requirement.Permission.StartsWith("Grpc"))
            {
                context.Fail(new AuthorizationFailureReason(this,"Grpc"));
                //context.Fail();
                return Task.CompletedTask;
            }
            //if (!value.Value.Equals(requirement) && requirement.Permission.StartsWith("Grpc"))
            //{
            //    context.Fail(new AuthorizationFailureReason(this, "Grpc"));
            //    //context.Fail();
            //    return Task.CompletedTask;
            //}
            //if (!string.IsNullOrWhiteSpace(value.Value))
            //{
            //    context.Succeed(requirement);
            //}
            return Task.CompletedTask;
        }
    }
}
